<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>类对象样式绑定</title>
        <style>
            .demo {
                width: 100px;
                height: 100px;
                background-color: gray;
                display: inline-block;
                margin: 10px;
            }

            .red {
                background-color: red;
            }

            .green {
                background-color: green;
            }

            .blue {
                background-color: blue;
            }
        </style>
        <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.js"></script>
    </head>

    <body>
        <div id="app">
            {{attachRed}}
            <div class="demo" @click=" attachRed =!attachRed"></div>
            <!-- TODO:理解动态样式绑定与对象绑定时的键名键值设置内容 -->
            <!-- 1.数据类型为对象类型,其键名是样式名称,键值是布尔值, 如果为true则样式添加,如果为false样式去除-->
            <div class="demo" :class="{red:attachRed}"></div>
            <div class="demo" :class="divClasses"></div>
            <!-- 2数据类型为数组类型 -->
            <div class="demo" :class="[color,{red:attachRed}]"></div>
            <!-- 3数据类型为字符串 -->
            {{color}}
            <input type="text" v-model="color">
        </div>
        <script type="text/javascript">
            Vue.config.productionTip = false;

            new Vue({
                el: '#app',
                data: {
                    attachRed:false,
                    color:'yellow',
                },
                computed:{
                    divClasses(){
                        return {
                            red:this.attachRed,
                            blue:!this.attachRed,
                        }
                    }
                }
            });
        </script>
    </body>
</html>
